package com.yhp.dao.impl;

import com.yhp.bean.Role;
import com.yhp.dao.RoleDao;
import com.yhp.util.Dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends Dbutils implements RoleDao {
    @Override
    public List<Role> getRole(int pageIndex,int pageSize) {
        List list = null;
        try {
            list = new ArrayList();
            String sql = "SELECT * FROM role  limit ?,?";
            List param = new ArrayList();
            param.add((pageIndex-1)*pageSize);
            param.add(pageSize);
            ResultSet query = query(sql, param);
            while (query.next()){
                Role role = new Role();
                role.setRoleId(query.getInt("roleid"));
                role.setRoleName(query.getString("rolename"));
                role.setRoleState(query.getInt("rolestate"));
                list.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return list;
    }

    @Override
    public int total() {
        int num =0;
        try {
            String sql = "SELECT count(*) FROM role  ";
            ResultSet query = query(sql, null);
            while (query.next()){
               num= query.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return num;
    }

    @Override
    public int addRole(Role role) {
        int id = 0;
        try {
            id = 0;
            String sql = "insert into role values(null,?,?)";
            List param = new ArrayList();
            param.add(role.getRoleName());
            param.add(role.getRoleState());
            update(sql,param);
            String sql1 = "select * from role where rolename=?";
            List param1 = new ArrayList();
            param1.add(role.getRoleName());
            ResultSet query = query(sql1, param1);
            while (query.next()){
                id=query.getInt("roleid");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return id;
    }

    @Override
    public int deleteRole(int id) {
        int update = 0;
        try {
            String sql = "delete from role where roleid=?";
            List param = new ArrayList();
            param.add(id);
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }

    @Override
    public List<Role> getRoleGo() {
        List list = null;
        try {
            list = new ArrayList();
            String sql = "SELECT * FROM role where rolestate !=0";
            ResultSet query = query(sql, null);
            while (query.next()){
                Role role = new Role();
                role.setRoleId(query.getInt("roleid"));
                role.setRoleName(query.getString("rolename"));
                role.setRoleState(query.getInt("rolestate"));
                list.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return list;
    }

    @Override
    public int ord(int id, int state) {
        String sql = "update role set rolestate = ? where roleid=?";
        List param = new ArrayList();
        param.add(state);
        param.add(id);
        int update = update(sql, param);
        return update;
    }


    @Override
    public Role findById(int id) {
        Role role = null;
        try {
            String sql = "SELECT * FROM role where roleid=?";
            List param = new ArrayList();
            param.add(id);
            ResultSet query = query(sql, param);
            role = new Role();
            while (query.next()){
                role.setRoleId(query.getInt("roleid"));
                role.setRoleName(query.getString("rolename"));
                role.setRoleState(query.getInt("rolestate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return role;
    }

    @Override
    public int updateRole(Role role) {
        int update = 0;
        try {
            String sql = "update role set rolename=?,rolestate=? where roleid=?";
            List param = new ArrayList();
            param.add(role.getRoleName());
            param.add(role.getRoleState());
            param.add(role.getRoleId());
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }
}
